package net.citiport.sample;

import java.io.IOException;
import java.lang.StringBuffer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.citiport.ggltranslate.core.LangConst;
import net.citiport.ggltranslate.event.GglTranslateEvent;
import net.citiport.ggltranslate.event.GglTranslateListener;
import net.citiport.ggltranslate.ggl.GoogleTranslator;
import net.citiport.ggltranslate.ggl.TranslatedPost;

/**
 * Servlet implementation class translateSample
 */
public class translateSample extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public translateSample() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    
    private class FooListener implements GglTranslateListener{
    	
		@Override
		public void onTranslated(GglTranslateEvent evt) {
			if(evt.getType() == GglTranslateEvent.CONTENTS_TRANSLATED){
				System.out.println("CONTENT"+evt.getPost().getText());
			}
		}
    	
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		GoogleTranslator translator = new GoogleTranslator();
		translator.addGglTranslateListener(new FooListener());
		TranslatedPost post = translator.GetTranslatedPost("hello world", LangConst.ENGLISH, LangConst.CHINESE);
		response.setCharacterEncoding(GoogleTranslator.getTextCharset());//@since 0.0.2
		response.getWriter().append(post.getText());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
